package com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest

import android.util.Log
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.remote.EvalService
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.remote.MarkService
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.remote.StudyAnalysisService
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.repo.EvalRepo
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.repo.MarkRepo
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.repo.StudyAnalysisRepo
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel.EvalViewModel
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel.MarkViewModel
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel.StuStudyAnalysisViewModel
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel.StudyAnalysisViewModel
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.util.concurrent.TimeUnit

val viewModelModule = module {
    viewModel { MarkViewModel(get()) }
    viewModel { EvalViewModel(get()) }
    viewModel { StudyAnalysisViewModel(get()) }
    viewModel { StuStudyAnalysisViewModel(get()) }
}

val repoModule = module {
    factory { EvalRepo(get()) }
    factory { MarkRepo(get()) }
    factory { StudyAnalysisRepo(get()) }
}

val remoteModule = module {
    val httpLoggingInterceptor = HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
        override fun log(message: String) {
            Log.e("==HTTP==", "拦截器骚操作message:" + message)
        }
    })
    httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)

    val client =
            OkHttpClient
                    .Builder()
                    .addInterceptor(MoreBaseUrlInterceptor())
                    .addNetworkInterceptor(httpLoggingInterceptor)
                    .addInterceptor(AiInterceptor())
                    .connectTimeout(120, TimeUnit.SECONDS)
                    .readTimeout(120, TimeUnit.SECONDS)
                    .writeTimeout(120, TimeUnit.SECONDS)
                    .build()

    single<Retrofit> {
        Retrofit.Builder()
                .baseUrl(Ext.PLATFORM_URL)
                .client(client)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
    }
    single<MarkService> { get<Retrofit>().create(MarkService::class.java) }
    single<EvalService> { get<Retrofit>().create(EvalService::class.java) }
    single<StudyAnalysisService> { get<Retrofit>().create(StudyAnalysisService::class.java) }

}

val appModule = listOf(viewModelModule, repoModule, remoteModule)



